package com.rd.student.jiaji.class2.shirui.day0728;

import java.util.*;

public class TestRandomNumber {
    public static void main(String[] args) {
        TestRandomNumber t = new TestRandomNumber();
        t.test1(20);
    }

    //数组去重1
    public void test1(int n) {
        int[] indexArr = new int[50];
        int cnt = 0;

        for (int i = 0; i < n; i++) {
            int index = (int) (Math.random() * 50) + 1;
            boolean flag = false;  // 判断是否已经随机过

            for (int j = 0; j < cnt; j++) {  // 遍历，判断当前的index是否已经被选过
                if (index == indexArr[j]) {
                    flag = true;
                    break;
                }
            }

            if (!flag) {  // 没有，添加数组中
                indexArr[cnt] = index;
                cnt++;
            }

            if (flag) {
                i--;
                continue;
            }

            System.out.println(index);

        }
    }

    // 数组去重2
    public void test2(int n) {
        int[] indexArr1 = new int[50];
        for (int i = 0; i < indexArr1.length; i++) {
            indexArr1[i] = -1;
        }

        for (int i = 0; i < n; i++) {
            // [1, 50]
            int random = (int) (Math.random() * 50) + 1;

            if (indexArr1[random] == -1) {
                // 代表random没有出现过
                indexArr1[random] = random;
                System.out.println("本次随机出：" + random);
                System.out.println(random);
                System.out.println(Arrays.toString(indexArr1));
            } else {
                // 代表已经出现过，这一次随机不生效
                System.out.println("已经随机过：" + random);
                i--;
            }
        }
    }

    // set
    public void test3(int n) {
        Set<Integer> set = new HashSet<>();
        Random random = new Random();
        while (set.size() < n) {
            int randomNumber = random.nextInt(51);
            set.add(randomNumber);
        }

        for (int number : set) {
            System.out.println(number);
        }
    }

    //shuffle
    public void test4(int n) {
        List<Integer> numbers = new ArrayList<>();

        for (int i = 0; i < 51; i++) {
            numbers.add(i);
        }

        Collections.shuffle(numbers);

        for (int i = 0; i < n; i++) {
            System.out.println(numbers.get(i));
        }
    }

    //随机一个去除一个
    public void test5(int n) {

        List<Integer> numbers = new ArrayList<>();

        for (int i = 1; i <= 50; i++) {
            numbers.add(i);
        }


        for (int i = 0; i < n; i++) {
            int randomIndex = (int) (Math.random() * numbers.size());
            int randomNum = numbers.get(randomIndex);
            System.out.println(randomNum);
            numbers.remove(randomIndex);
        }

    }


}
